/**
 * @ Author: luoqi
 * @ Create Time: 2024-11-12 22:27
 * @ Modified by: luoqi
 * @ Modified time: 2025-03-10 23:09
 * @ Description:
 */

#include <bsp.h>
#include "mc.h"
#include "../dev/qdrive.h"
#include "../modules/qfoc.h"
#include "../qshell/qterm.h"

static QDrive *drv;
static QFocObj *foc1, *foc2;

static QCliCmd cmd_mc;
static int cmd_mc_hdl(int argc, char **argv);

static TX_THREAD task_mc;
static uint8_t task_mc_stack[1024];

static void task_mc_hdl(uint32_t args)
{
    UNUSED(args);
    while(1) {
        tx_thread_sleep(1000);
    }
}


int mc_init(void)
{
    drv = qdrv_get();
    foc1 = &drv->foc1;
    foc2 = &drv->foc2;

    tx_thread_create(&task_mc, "mc", task_mc_hdl, 0, task_mc_stack, sizeof(task_mc_stack), 6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
    qterm_attach(&cmd_mc, "mc", cmd_mc_hdl, "motor control commands");
    return 0;
}

static int args_status_hdl(int argc, char **argv)
{
    UNUSED(argc);
    UNUSED(argv);
    return 0;
}

static QCliArgsEntry mc_args_table[] = {
    {"status", 1, 1, args_status_hdl, "show motor controller status"},
};

static int cmd_mc_hdl(int argc, char **argv)
{
    if(ISARG(argv[1], "?") && argc == 2) {
        return qterm_help(mc_args_table, sizeof(mc_args_table));
    }
    return qcli_args_handle(argc, argv, mc_args_table, sizeof(mc_args_table));
}
